/*
 * @Author: 0x9DEFA478
 * @Date: 2025-10-03 21:25:17
 * @LastEditTime: 2025-10-05 19:03:49
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * 
 */
#ifndef hSystemAPI_Windows_Handle_HPP_
#define hSystemAPI_Windows_Handle_HPP_
#if defined(_WIN32)
#include <windows.h>
#include <functional>
#endif


namespace SystemAPI{

  namespace Windows{

#if defined(_WIN32)
    class Handle{
      public:
        using Close_t=std::function<void (HANDLE)>;

      public:
        Handle()noexcept:handle(INVALID_HANDLE_VALUE){}

        template<typename F_Delete=Close_t>
        Handle(HANDLE handle,F_Delete&& f_delete=default_delete):handle(handle),f_delete(std::forward<F_Delete>(f_delete)){}

        ~Handle()noexcept{
          if(handle!=INVALID_HANDLE_VALUE){
            f_delete(handle);
          }
        }

        Handle(const Handle&)=delete;
        Handle(Handle&& handle)noexcept:handle(handle.handle),f_delete(std::move(handle.f_delete)){
          handle.handle=INVALID_HANDLE_VALUE;
        }

        Handle& operator=(const Handle&)=delete;
        Handle& operator=(Handle&& handle)noexcept{
          if(this->handle!=INVALID_HANDLE_VALUE){
            f_delete(this->handle);
          }
          this->handle=handle.handle;
          this->f_delete=std::move(handle.f_delete);
          handle.handle=INVALID_HANDLE_VALUE;
          return *this;
        }

        HANDLE GetHandle()const noexcept{return handle;}

        static void default_delete(HANDLE handle)noexcept{CloseHandle(handle);}

      private:
        HANDLE handle;
        Close_t f_delete;
    };
#endif

  }

}


#endif //hSystemAPI_Windows_Handle_HPP_
